<template>
  <div>
    <el-form ref="tableForm" :inline="true" :model="tableForm" class="demo-ruleForm">
      <el-form-item label="用户">
        <user-id-select v-model="tableForm.requestUserId"></user-id-select>
      </el-form-item>
      <el-form-item label="请求地址">
        <el-input v-model="tableForm.requestUri"/>
      </el-form-item>
      <el-form-item label="请求方法">
        <el-input v-model="tableForm.requestMethod"/>
      </el-form-item>
      <el-form-item label="请求类型">
        <el-input v-model="tableForm.requestContentType"/>
      </el-form-item>
      <el-form-item label="请求参数">
        <el-input v-model="tableForm.requestParams"/>
      </el-form-item>
      <el-form-item label="请求IP">
        <el-input v-model="tableForm.requestIp"/>
      </el-form-item>
      <el-form-item label="调用方法">
        <el-input v-model="tableForm.invokeMethodName"/>
      </el-form-item>

      <el-form-item label="请求时间">
        <el-date-picker
          v-model="tableForm.requestTimeRange" end-placeholder="结束日期" range-separator="至"
          start-placeholder="开始日期" type="datetimerange"
        />
      </el-form-item>
      <el-button type="primary" @click="page()"> 查询</el-button>
      <el-button @click="resetPageRequestBody()"> 重置</el-button>
    </el-form>

    <el-table :data="tableList" border>
      <el-table-column fixed label="ID" prop="id" min-width="50"/>
      <el-table-column label="用户" prop="requestUser.cn" min-width="50"/>
      <el-table-column label="请求地址" prop="requestUri" min-width="100"/>
      <el-table-column label="请求方法" prop="requestMethod" min-width="60"/>
      <el-table-column label="请求类型" prop="requestContentType" min-width="80"/>
      <el-table-column label="请求参数" prop="requestParams" min-width="100">
        <template slot-scope='scope'>
          <el-popover placement="top-start" width="400" trigger="hover">
            <span>{{ scope.row.requestParams }}</span>
            <span slot="reference">{{ scope.row.requestParams.substr(0, 30) + '...' }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="请求IP" prop="requestIp" min-width="70"/>
      <el-table-column label="调用方法" prop="invokeMethodName" min-width="100">
        <template slot-scope='scope'>
          <el-popover placement="top-start" width="400" trigger="hover">
            <span>{{ scope.row.invokeMethodName }}</span>
            <span slot="reference">{{ scope.row.invokeMethodName.substr(0, 30) + '...' }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="请求时间" prop="createTime" min-width="120">
        <template v-slot="scope">
          {{ scope.row.requestTime|transformDateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="响应时间" prop="updateTime" min-width="120">
        <template v-slot="scope">
          {{ scope.row.responseTime|transformDateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="请求体" width="75">
        <template v-slot="scope">
          <el-button type="text" size="small" @click="sendRequestBody(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="响应体" width="75">
        <template v-slot="scope">
          <el-button type="text" size="small" @click="sendResponseBody(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination :current-page="pageForm.current" :total="pageForm.total" :page-size="pageForm.size"
                   :page-sizes="pageForm.sizes"
                   :style="pageForm.style" :layout="pageForm.layout"
                   @current-change="page" @size-change="changeSize"/>
  </div>
</template>
<script>
import {page, requestBody, responseBody} from "@/api/system/audit";
import BdpPagination from "@/views/common/bdpPagination";
import {createPageRequestBody} from "@/api/system/pagination";
import UserIdSelect from "@/views/system/user/userIdSelect";

export default {
  name: "AuditLogIndex",

  components: {UserIdSelect, BdpPagination},

  data() {
    return {
      tableForm: {},
      pageForm: createPageRequestBody(),
      tableList: [],
    }
  },

  mounted() {
    this.page()
  },

  methods: {
    page(current = 1) {
      this.pageForm.current = current
      page(this.tableForm, this.pageForm.current, this.pageForm.size).then(res => {
        this.tableList = res.data.records
        this.pageForm.total = res.data.total
      })
    },
    changeSize(size) {
      this.pageForm.size = size
      this.page(1)
    },
    sendRequestBody(id) {
      requestBody(id).then(() => {
        this.$message({message: "已发送至您的邮箱，请查收！", type: "success"})
      }).catch(() => {
        this.$message({message: "查看失败，请联系管理员！", type: "error"})
      })
    },
    sendResponseBody(id) {
      responseBody(id).then(() => {
        this.$message({message: "已发送至您的邮箱，请查收！", type: "success"})
      }).catch(() => {
        this.$message({message: "查看失败，请联系管理员！", type: "error"})
      })
    },
    resetPageRequestBody() {
      this.tableForm = {}
      this.pageForm = createPageRequestBody()
      this.page()
    },
  },
}
</script>

<style scoped>

</style>
